package com.agile.major.service.impl;

import com.agile.major.domain.MajorTraning;
import com.agile.major.domain.TraningDataHistory;
import com.agile.major.repository.TraningDataHistoryRepository;
import com.agile.major.service.MajorTraningService;
import com.agile.major.service.TraningDataHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing TraningDataHistory.
 */
@Service
@Transactional
public class TraningDataHistoryServiceImpl implements TraningDataHistoryService{

    private final Logger log = LoggerFactory.getLogger(TraningDataHistoryServiceImpl.class);

    private final TraningDataHistoryRepository traningDataHistoryRepository;
    @Autowired
    MajorTraningService majorTraningService;

    public TraningDataHistoryServiceImpl(TraningDataHistoryRepository traningDataHistoryRepository) {
        this.traningDataHistoryRepository = traningDataHistoryRepository;
    }

    /**
     * Save a traningDataHistory.
     *
     * @param traningDataHistory the entity to save
     * @return the persisted entity
     */
    @Override
    public TraningDataHistory save(TraningDataHistory traningDataHistory) {
        log.debug("Request to save TraningDataHistory : {}", traningDataHistory);
        return traningDataHistoryRepository.save(traningDataHistory);
    }

    /**
     *  Get all the traningDataHistories.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TraningDataHistory> findAll(Pageable pageable) {
        log.debug("Request to get all TraningDataHistories");
        return traningDataHistoryRepository.findAll(pageable);
    }

    /**
     *  Get one traningDataHistory by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public TraningDataHistory findOne(Long id) {
        log.debug("Request to get TraningDataHistory : {}", id);
        return traningDataHistoryRepository.findOne(id);
    }

    /**
     *  Delete the  traningDataHistory by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TraningDataHistory : {}", id);
        traningDataHistoryRepository.delete(id);
    }

    @Override
    public Page<TraningDataHistory> findByTraning(Long traningId, Pageable page){
        MajorTraning majorTraning=majorTraningService.findOne(traningId);
        if(majorTraning==null){
            log.debug("没有找到版本,编号为"+traningId+"的历史记录");
        }
        return traningDataHistoryRepository.findTraningDataHistoriesByTraning(majorTraning,page);
    }
}
